﻿Public Class frmSearchData
    Dim SearchClick As Boolean = False
    Dim CurrentSearchInfo As SearchData
    Private Sub OrderNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrderNo.TextChanged
        Btn_Search_Click(sender, e)
    End Sub

    Private Sub SearchData_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        If SearchClick = False Then SearchResult = Nothing
        CurrentSearchData = CurrentSearchInfo
        SaveData.LastSearchTable = Me.TableName.Text
        e.Cancel = False
    End Sub

    Private Sub SearchData_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        On Error Resume Next
        Me.TableName.Items.Clear()
        TableName.Items.Add("RemedyCode")
        If SaveData.SearchTable IsNot Nothing Then '加载保存的数据表
            For i = 0 To SaveData.SearchTable.Count - 1
                Me.TableName.Items.Add(SaveData.SearchTable.Item(i).TableName)
            Next
        End If
        If Not String.IsNullOrEmpty(SaveData.LastSearchTable) Then '加载上次访问的数据表
            For i = 0 To TableName.Items.Count - 1
                If TableName.Items.Item(i).ToString.ToUpper = SaveData.LastSearchTable.ToUpper Then
                    TableName.SelectedIndex = i
                    Exit For
                End If
            Next
        End If
        Me.Show()
        If Me.TableName.Items.Count > 0 Then '加载数据结构表
            Application.DoEvents()
            SearchData("Searching")
        End If
        With DataView
            .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing
            .ColumnHeadersHeight = 30
            .ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
        End With
        ChangeButtonColor(Btn_Search)
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If Asc(e.KeyChar) <> 8 Then
            If Not IsNumeric(e.KeyChar) Then e.Handled = True
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub Btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Search.Click
        SearchData(OrderNo.Text)
    End Sub
    Private Sub SearchData(ByVal SearchValue As String)
        Try
            Dim FiledStr As String
            Dim SqlString As String
            If TableName.Text.ToUpper = UCase("RemedyCode") Then
                FiledStr = "RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName"
                SqlString = "select " & FiledStr & " from " & TableName.Text & " where workbillno like '%" & SearchValue & "%'"
            Else
                SqlString = CurrentSearchInfo.SearchSql & "'%" & SearchValue & "%'"
            End If
            Dim ds As DataSet, TmpStr As String = ""
            ds = Sql.Search(TableName.Text, SqlString)
            DataView.DataSource = ds.Tables(0).DefaultView
            SearchClick = False
        Catch ex As Exception
            'Me.Text = ex.Message
        End Try
    End Sub
    Private Sub SearchByData()
        Try
            Dim FiledStr As String
            Dim SqlString As String
            If TableName.Text.ToUpper = UCase("RemedyCode") Then
                FiledStr = "RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName"
                SqlString = "select " & FiledStr & " from " & TableName.Text & " where workbillno like '%" & OrderNo.Text & "%'"
            Else
                SqlString = CurrentSearchInfo.SearchSql & "'%" & OrderNo.Text & "%'"
            End If
            Dim ds As DataSet, TmpStr As String = ""
            ds = Sql.Search(TableName.Text, SqlString)
            DataView.DataSource = ds.Tables(0).DefaultView
            SearchClick = False
        Catch

        End Try
    End Sub
    Private Sub ShowWorkBillHeaderText()
        On Error Resume Next
        With DataView
            .Columns("WorkBillID").HeaderText = "编号"
            .Columns("WorkBillNo").HeaderText = "工单号"
            .Columns("UniteQty").HeaderText = "联数"
            .Columns("ProductNo").HeaderText = "产品编号"
            .Columns("ProductName").HeaderText = "产品名称"
            .Columns("StartNo").HeaderText = "起始号码"
            .Columns("EndNo").HeaderText = "终止号码"
            .Columns("StepLength").HeaderText = "步长值"
            .Columns("CheckRules").HeaderText = "校验方式"
        End With
    End Sub
    Private Sub ShowRemedyCodeHeaderText()
        On Error Resume Next
        With DataView
            '"RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName"
            .Columns("RemedycodeID").HeaderText = "编号"
            .Columns("WorkBillNo").HeaderText = "工单号"
            .Columns("StartNo").HeaderText = "起始号码"
            .Columns("EndNo").HeaderText = "终止号码"
            .Columns("UniteNo").HeaderText = "联次"
            .Columns("StepLength").HeaderText = "步长"
            .Columns("Prefixion").HeaderText = "前缀"
            .Columns("Suffix").HeaderText = "后缀"
            .Columns("ProductNo").HeaderText = "产品编号"
            .Columns("ProductName").HeaderText = "产品名称"
        End With
    End Sub
    Public Sub SetTableDescription(ByVal mTableName As String)

        Dim sqlStr1 As String = "SELECT   * FROM   ::fn_listextendedproperty (null, 'user', 'dbo', 'table','" + mTableName + "', 'column', default)"
        Dim tmpDs As DataSet
        Try

            tmpDs = Sql.Search(mTableName, sqlStr1)
            Dim DC As Integer = tmpDs.Tables(0).Rows.Count - 1
            Dim mField As String = vbNull
            For i = 0 To DC Step 1
                Try

                    mField = tmpDs.Tables(0).Rows(i)(1).ToString()
                    DataView.Columns(mField).HeaderText = tmpDs.Tables(0).Rows(i)(3).ToString()
                Catch
                End Try
            Next
        Catch
        End Try
    End Sub
    Private Sub DataView_DataSourceChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataView.DataSourceChanged
        If TableName.Text.ToUpper = UCase("RemedyCode") Then
            ShowRemedyCodeHeaderText()
        Else
            SetTableDescription(DirectCast(DataView.DataSource, DataView).Table.TableName)
        End If
    End Sub

    Private Sub DataView_RowHeaderMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataView.RowHeaderMouseDoubleClick
        Dim DV As DataView = DataView.DataSource
        Dim tName As String = DV.Table.TableName
        Dim CurRow As DataGridViewRow = DataView.CurrentRow
        LoadData(tName, CurRow)
        SearchClick = True
    End Sub
    Public Sub LoadData(ByVal sTableName As String, ByVal SelectRow As DataGridViewRow)
        Try
            Dim SqlStr As String = ""
            SearchResult = Nothing
            If UCase(sTableName) = UCase("RemedyCode") Then
                'RemedycodeID,WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix,ProductNo,ProductName
                Dim Order As String, UniteN As String, Rds As DataSet
                Order = SelectRow.Cells("WorkBillNo").Value.ToString
                UniteN = SelectRow.Cells("UniteNo").Value.ToString
                SqlStr = "select WorkBillNo,StartNo,EndNo,UniteNo,StepLength,Prefixion,Suffix from RemedyCode where workbillno='" & Order & "' and UniteNo='" & UniteN & "'"
                Rds = Sql.Search("RemedyCode", SqlStr)
                If Rds Is Nothing Then
                    MsgBox("查找错误，找不到数据！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
                    Return
                ElseIf Rds.Tables(0).Rows.Count <= 0 Then
                    MsgBox("查找错误，找不到数据！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
                    Return
                Else
                    SearchResult = Rds.Tables(0)
                    SearchClick = True
                    Me.Close()
                End If
            Else
                Dim SqlTJ() As String
                Dim a As Integer, b As Integer, TJ As String = ""
                Dim tmp As String = CurrentSearchInfo.DownLoadDataSql
                Dim tjTmp As String, tjValue As String, Rds As DataSet
                a = tmp.LastIndexOf("[")
                b = tmp.LastIndexOf("]")
                If b > a And a > 0 Then '查找条件，并构建条件语句
                    SqlStr = tmp.Substring(0, a)
                    Try
                        tjTmp = tmp.Substring(a + 1, b - a - 1)
                    Catch ex As Exception
                        tjTmp = ""
                    End Try
                    SqlTJ = tjTmp.Split(",")
                    For i = 0 To SqlTJ.Length - 1
                        Try
                            tjValue = SelectRow.Cells(SqlTJ(i)).Value.ToString
                            If TJ <> "" Then
                                TJ = TJ & " and " & SqlTJ(i).Trim & " ='" & tjValue & "'"
                            Else
                                TJ = SqlTJ(i).Trim & " ='" & tjValue & "'"
                            End If
                        Catch
                        End Try
                    Next
                    SqlStr = SqlStr & " " & TJ
                End If
                Rds = Sql.Search(sTableName, SqlStr)
                If Rds Is Nothing Then
                    MsgBox("查找错误，找不到数据！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
                    Return
                ElseIf Rds.Tables(0).Rows.Count <= 0 Then
                    MsgBox("查找错误，找不到数据！", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "提示")
                    Return
                Else
                    SearchResult = Rds.Tables(0)
                    SearchClick = True
                    Me.Close()
                End If
            End If
        Catch EX As Exception
            MsgBox(EX.Message, MsgBoxStyle.Critical, "错误")
        End Try
    End Sub

    Private Sub DataView_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataView.CellContentClick

    End Sub

    Private Sub TableName_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TableName.SelectedIndexChanged
        Dim tmpSelected As String = TableName.Items.Item(TableName.SelectedIndex).ToString
        CurrentSearchInfo = Nothing
        If tmpSelected.ToUpper <> "RemedyCode".ToUpper Then
            If SaveData.SearchTable IsNot Nothing Then
                For i = 0 To SaveData.SearchTable.Count - 1
                    If tmpSelected.ToUpper = SaveData.SearchTable.Item(i).TableName.ToUpper Then
                        CurrentSearchInfo = SaveData.SearchTable.Item(i)
                        Exit For
                    End If
                Next
            End If
        End If
    End Sub
End Class